package com.linksfield.topup.controller.accountmanage;

import com.alibaba.fastjson.JSON;
import com.linksfield.topup.config.dto.ResultDataDto;
import com.linksfield.topup.config.dto.StringOutputConstant;
import com.linksfield.topup.entity.interfaces.accountmanage.create.request.RequestCreateAccount;
import com.linksfield.topup.entity.interfaces.accountmanage.operate.request.RequestAccountOperate;
import com.linksfield.topup.entity.interfaces.accountmanage.search.request.RequestAccountSearch;
import com.linksfield.topup.entity.interfaces.accountmanage.search.response.ResponseAccountSearch;
import com.linksfield.topup.service.SysOperateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * 系统用户管理控制器类
 *
 * @author: lm
 * @date: 2019-12-18 15:30
 */
@RestController
@Api(value = "系统用户管理")

public class SysAccountManageController {

    private static Logger logger = Logger.getLogger(SysAccountManageController.class);

    @Autowired
    SysOperateService operateService;

    @CrossOrigin
    @PostMapping(value = "accountSearch", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "系统用户管理：搜索", response = ResponseAccountSearch.class)
    public ResultDataDto accountSearch(@ApiParam @RequestBody RequestAccountSearch request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【系统用户管理-搜索】请求：" + JSON.toJSONString(request));

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return operateService.accountSearch(request, token);

    }

    @CrossOrigin
    @PostMapping(value = "accountOperate", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "系统用户管理：操作：重置密码；封禁账号；启用账号")
    public ResultDataDto accountOperate(@ApiParam @RequestBody RequestAccountOperate request,
                                        @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【系统用户管理：操作】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return operateService.accountOperate(request, token);
    }

    @CrossOrigin
    @PostMapping(value = "createAccount", produces = "application/json;charset=utf-8")
    @ResponseBody
    @ApiOperation(value = "系统用户管理：创建用户账号")
    public ResultDataDto createAccount(@ApiParam @RequestBody RequestCreateAccount request,
                                       @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【系统用户管理：创建】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return operateService.createAccount(request, token);

    }


}
